<!--
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .button-container {
            text-align: center;
            margin-bottom: 20px;
        }
        .button {
            display: inline-block;
            background-color: #007BFF;
            color: white;
            padding: 10px 20px;
            text-decoration: none;
            border-radius: 5px;
            margin: 0 10px;
            transition: background-color 0.3s;
        }
        .button:hover {
            background-color: #0056b3;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            padding: 10px;
            border: 1px solid #ddd;
            text-align: left;
        }
        th {
            background-color: #007BFF;
            color: white;
        }
    </style>
</head>
<body>
<h1>用户管理</h1>
<div class="button-container">
    <a href="#" class="button" onclick="queryUsers()">查询用户</a>
    <a href="#" class="button">修改用户</a>
</div>
<table id="userTable">
    <thead>
    <tr>
        <th>ID</th>
        <th>用户名</th>
        <th>邮箱</th>
        <th>创建时间</th>
        <th>是否管理员</th>
    </tr>
    </thead>
    <tbody>
    &lt;!&ndash; 初始为空，等待查询结果填充 &ndash;&gt;
    </tbody>
</table>

<script>
    function queryUsers() {
        const xhr = new XMLHttpRequest();
        xhr.open('GET', '/usermanage/query', true);
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                const users = JSON.parse(xhr.responseText);
                const tableBody = document.getElementById('userTable').getElementsByTagName('tbody')[0];
                tableBody.innerHTML = '';
                users.forEach(user => {
                    const row = tableBody.insertRow();
                    const idCell = row.insertCell(0);
                    const usernameCell = row.insertCell(1);
                    const emailCell = row.insertCell(2);
                    const createTimeCell = row.insertCell(3);
                    const isAdminCell = row.insertCell(4);

                    idCell.textContent = user.id;
                    usernameCell.textContent = user.username;
                    emailCell.textContent = user.email;
                    createTimeCell.textContent = user.createTime;
                    isAdminCell.textContent = user.isAdmin;
                });
            }
        };
        xhr.send();
    }
</script>
</body>
</html>-->
<!-- src/main/resources/templates/usermanage.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .button-container {
            text-align: center;
            margin-bottom: 20px;
        }
        .button {
            display: inline-block;
            background-color: #007BFF;
            color: white;
            padding: 10px 20px;
            text-decoration: none;
            border-radius: 5px;
            margin: 0 10px;
            transition: background-color 0.3s;
        }
        .button:hover {
            background-color: #0056b3;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            padding: 10px;
            border: 1px solid #ddd;
            text-align: left;
        }
        th {
            background-color: #007BFF;
            color: white;
        }
        /* 添加模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0,0,0,0.4);
        }
        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
        }
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }
        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
<h1>用户管理</h1>
<div class="button-container">
    <a href="#" class="button" onclick="queryUsers()">查询用户</a>
</div>
<table id="userTable">
    <thead>
    <tr>
        <th>ID</th>
        <th>用户名</th>
        <th>邮箱</th>
        <th>创建时间</th>
        <th>是否管理员</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <!-- 初始为空，等待查询结果填充 -->
    </tbody>
</table>

<!-- 修改模态框 -->
<div id="updateModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <h2>修改用户信息</h2>
        <form id="updateForm">
            <input type="hidden" id="updateUserId" name="id"><br><br>
            <label for="updateUsername">用户名:</label>
            <input type="text" id="updateUsername" name="username" required><br><br>
            <label for="updatePassword">密码:</label>
            <input type="password" id="updatePassword" name="password" required><br><br>
            <label for="updateEmail">邮箱:</label>
            <input type="email" id="updateEmail" name="email" required><br><br>
            <label for="updateIsAdmin">是否管理员:</label>
            <input type="number" id="updateIsAdmin" name="isAdmin" min="0" max="1" required><br><br>
            <button type="button" onclick="saveChanges()">保存修改</button>
        </form>
    </div>
</div>

<script>
    function queryUsers() {
        const xhr = new XMLHttpRequest();
        xhr.open('GET', '/usermanage/query', true);
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                const users = JSON.parse(xhr.responseText);
                const tableBody = document.getElementById('userTable').getElementsByTagName('tbody')[0];
                tableBody.innerHTML = '';
                users.forEach(user => {
                    const row = tableBody.insertRow();
                    const idCell = row.insertCell(0);
                    const usernameCell = row.insertCell(1);
                    const emailCell = row.insertCell(2);
                    const createTimeCell = row.insertCell(3);
                    const isAdminCell = row.insertCell(4);
                    const actionCell = row.insertCell(5);

                    idCell.textContent = user.id;
                    usernameCell.textContent = user.username;
                    emailCell.textContent = user.email;
                    createTimeCell.textContent = user.createTime;
                    isAdminCell.textContent = user.isAdmin;

                    // 添加修改按钮
                    const updateButton = document.createElement('button');
                    updateButton.textContent = '修改';
                    updateButton.addEventListener('click', function() {
                        // 填充模态框表单
                        document.getElementById('updateUserId').value = user.id;
                        document.getElementById('updateUsername').value = user.username;
                        document.getElementById('updatePassword').value = user.password;
                        document.getElementById('updateEmail').value = user.email;
                        document.getElementById('updateIsAdmin').value = user.isAdmin;

                        // 显示模态框
                        const modal = document.getElementById('updateModal');
                        modal.style.display = 'block';
                    });
                    actionCell.appendChild(updateButton);
                });
            }
        };
        xhr.send();
    }

    // 关闭模态框
    const closeBtn = document.querySelector('.close');
    const modal = document.getElementById('updateModal');
    closeBtn.addEventListener('click', function() {
        modal.style.display = 'none';
    });

    window.addEventListener('click', function(event) {
        if (event.target === modal) {
            modal.style.display = 'none';
        }
    });

    function saveChanges() {
        const form = document.getElementById('updateForm');
        const formData = new FormData(form);
        const user = {};
        formData.forEach((value, key) => {
            user[key] = value;
        });

        const xhr = new XMLHttpRequest();
        xhr.open('POST', '/updateUser', true);
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    // 更新成功后关闭模态框并重新查询用户列表
                    modal.style.display = 'none';
                    queryUsers();
                } else {
                    alert('更新失败，请稍后重试');
                }
            }
        };
        xhr.send(JSON.stringify(user));
    }
</script>
</body>
</html>
